package com.nanjustar.business.business.security;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjustar.api.moudle.security.api.ConsumerService;
import com.nanjustar.api.moudle.security.dto.ConsumerInfoDto;
import com.nanjustar.api.moudle.security.entity.Consumer;
import com.nanjustar.api.moudle.security.entity.Menu;
import com.nanjustar.api.moudle.security.vo.ConsumerConditionVo;
import com.nanjustar.api.moudle.security.vo.ConsumerVo;
import com.nanjustar.api.moudle.security.vo.LoginVo;
import com.nanjustar.common.constant.ServiceErrorConst;
import com.nanjustar.common.exception.NanjustarException;
import com.nanjustar.common.utils.AssertUtil;
import com.nanjustar.common.utils.BeanCopyUtil;
import com.nanjustar.mapper.mapper.security.ConsumerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nanjustar
 * @since 2021-11-13
 */
@Slf4j
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Override
    public String userLogin(LoginVo loginVo) {
        log.info("loginVo is ================ "+loginVo);
        log.info(loginVo.getUsername());
        log.info(loginVo.getPassword());
        log.info(loginVo.getCaptcha());
        return null;
    }


    @Override
    public List<ConsumerInfoDto> listConsumerByCondition(ConsumerConditionVo consumerConditionVo) {
        List<Consumer> consumers = baseMapper.selectList(new LambdaQueryWrapper<Consumer>()
                .like(AssertUtil.isNotEmpty(consumerConditionVo.getUserName()),Consumer::getUsername,consumerConditionVo.getUserName())
                .like(AssertUtil.isNotEmpty(consumerConditionVo.getNickName()),Consumer::getNickname,consumerConditionVo.getNickName())
                .eq(AssertUtil.isNotEmpty(consumerConditionVo.getGender()), Consumer::getGender, consumerConditionVo.getGender())
        );
        List<ConsumerInfoDto> list = new ArrayList<>();
        consumers.forEach(consumer -> {
            ConsumerInfoDto consumerInfoDto = BeanCopyUtil.copyObject(consumer,ConsumerInfoDto.class);
            list.add(consumerInfoDto);
        });

        return list;
    }



    @Override
    public List<ConsumerInfoDto> listConsumerRouter() {
        /*------------------------  查询用户信息开始  -----------------------------*/
        List<Consumer> consumers = baseMapper.selectList(new LambdaQueryWrapper<Consumer>());

        List<ConsumerInfoDto> consumerInfoDtos = new ArrayList<>();
        consumers.forEach(consumer -> {
            ConsumerInfoDto consumerInfoDto = BeanCopyUtil.copyObject(consumer,ConsumerInfoDto.class);
            consumerInfoDtos.add(consumerInfoDto);
        });
        return consumerInfoDtos;
    }

    @Override
    public void saveConsumer(ConsumerVo consumerVo) {
        /*------------------------  开始查重用户是否存在  -----------------------------*/
        Integer count = baseMapper.selectCount(new LambdaQueryWrapper<Consumer>()
                .eq(Consumer::getUsername,consumerVo.getUsername())
        );
        //密码加密
        String passWord = consumerVo.getPassword();
        BCryptPasswordEncoder bcp = new BCryptPasswordEncoder();
        passWord = bcp.encode(passWord);
        consumerVo.setPassword(passWord);

        /*------------------------  查重用户是否存在完成  -----------------------------*/
        /*------------------------  开始新增用户  -----------------------------*/
        int insert = baseMapper.insert(BeanCopyUtil.copyObject(consumerVo, Consumer.class));
        AssertUtil.sysIsError(count > 0, ServiceErrorConst.DATA_ALREADY_EXISTS);
        AssertUtil.sysIsError(insert == 0, ServiceErrorConst.SAVE_DATA_FAIL);
        /*------------------------  新增用户 完成  -----------------------------*/
    }

    @Transactional(rollbackFor = NanjustarException.class)
    @Override
    public void updateConsumer(ConsumerVo consumerVo) {
        /*------------------------  开始查询是否存在  -----------------------------*/
        Integer count = baseMapper.selectCount(new LambdaQueryWrapper<Consumer>()
                .eq(Consumer::getUserId,consumerVo.getUserId())
        );
        AssertUtil.sysIsError(count > 1, ServiceErrorConst.DATA_ALREADY_EXISTS);
        /*------------------------  查重用户是否存在完成  -----------------------------*/
        /*------------------------  修改用户信息  -----------------------------*/
        int update = baseMapper.updateById(BeanCopyUtil.copyObject(consumerVo, Consumer.class));
        AssertUtil.sysIsError(update == 0, ServiceErrorConst.UPDATE_DATA_FAIL);
        /*------------------------  修改用户信息完成  -----------------------------*/
    }

    @Transactional(rollbackFor = NanjustarException.class)
    @Override
    public void deleteConsumer(Integer id) {
        /*------------------------  开始删除用户 -----------------------------*/
        int delete = baseMapper.deleteById(id);
        AssertUtil.sysIsError(delete == 0, ServiceErrorConst.DELETE_DATA_FAIL);
        /*------------------------  删除用户完成  -----------------------------*/

        /*------------------------  开始删除用户已经分配的角色 -----------------------------*/
        /*
        * 后续开发
        * */
    }
}
